//
//  DHGoodListViewModel.m
//  宏达APP
//
//  Created by DIANGHONG on 2018/4/29.
//  Copyright © 2018年 123. All rights reserved.
//

#import "DHGoodListViewModel.h"
#import "DHNetworkManager+DHHodaNetworkManager.h"
// ViewModel
@implementation DHGoodListViewModel


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.page = 0;
    }
    return self;
}

- (NSArray *)listModel{
    if (!_listModel) {
        _listModel = [[NSArray alloc] init];
    }
    return _listModel;
}

- (void)loadConfigWithPlanID:(NSInteger)planID completion:(void(^)(BOOL isSuccess))completion{
    
    [DHNetworkManager.shareManager loadConfigListWithPlan_id:@(planID) completion:^(BOOL isSuccess, NSArray *datas) {
        
        if (!isSuccess) {
            completion(isSuccess);
            return ;
        }
        
        if (!datas) {
            completion(false);
        }
        
        self.configList = datas;
        completion(isSuccess);
    }];
}

/**
 加载界面数据

 @param pullUP 是否上拉刷新
 @param finishedBlock 完成回调
 */
- (void)loadGoodListIsPullUP:(BOOL)pullUP key:(NSNumber *)key oder:(NSNumber *)oder plan_id:(NSNumber *)plan_id gc_id:(NSNumber *)gc_id finishedBlock:(void(^)(BOOL Success,BOOL noMoreData))finishedBlock{
    
    if (pullUP) {
        self.page += 1;
        //self.curPage = self.page;
    }
    // 判断重复下拉刷新
    if (self.page == 1) {
        finishedBlock(NO,NO);
        return;
    }

    if (self.page >= 15) {
        //completion(YES,NO);
        finishedBlock(YES,YES);
        return;
    }
    
    [DHNetworkManager.shareManager requestGoodListWithKey:key order:oder page:@20 curPage:@(self.page) gc_id:gc_id store_id:nil keyword:nil b_id:nil own_shop:NO gift:NO groupbuy:NO xianshi:NO virtual:NO a_id:nil price_from:nil price_to:nil plan_id:plan_id completion:^(NSArray *goodList, NSArray *planList, DHPlanInfo *planInfo, BOOL isSuccess) {
        
            if (!isSuccess) {
                    return ;
            }
            if (!goodList) {
                finishedBlock(YES,YES);
            return;
            }

            self.listModel = [self.listModel arrayByAddingObjectsFromArray:goodList];
            self.planList = planList;
            self.planInfo = planInfo;
        
        
                if (!pullUP && isSuccess && self.listModel.count > 0) {
                    self.page = 1;
                }
        
                // completion(isSuccess,noMoreData);
                finishedBlock(isSuccess,NO);
        
    }];
    
//    [DHNetworkManager.shareManager requestGoodListWithKey:key order:nil page:@20 curPage:@(self.page) gc_id:gc_id store_id:nil keyword:nil b_id:nil own_shop:NO gift:NO groupbuy:NO xianshi:NO virtual:NO a_id:nil price_from:nil price_to:nil plan_id:plan_id completion:^(NSArray *model, BOOL isSuccess) {
//        
//        if (!isSuccess) {
//            return ;
//        }
//        
//        self.listModel = [self.listModel arrayByAddingObjectsFromArray:model];
//        if (!pullUP && isSuccess && self.listModel.count > 0) {
//            self.page = 1;
//        }
//        
//        // completion(isSuccess,noMoreData);
//        finishedBlock(isSuccess,NO);
//
//    }];
    
    
}

@end
